package airport.client.widget.panneau;

import airport.client.Airport;
import airport.client.utils.Constantes;
import airport.shared.beans.HotelBean;
import airport.shared.beans.PanneauBean;
import airport.shared.beans.UserBean;
import airport.shared.beans.VolBean;
import airport.shared.service.*;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.*;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Cyril on 04/03/2015.
 */
public class PanneauWB extends Airport {

	FlowPanel mainPanel = new FlowPanel();
	FlowPanel consultPanel = new FlowPanel();
	FlowPanel ajoutPanel = new FlowPanel();
	FlowPanel updatePanel = new FlowPanel();
    FlowPanel volPanel = new FlowPanel();
    FlowPanel hotelPanel = new FlowPanel();
	FlowPanel panneauPanel = new FlowPanel();
	FlowPanel datePanel = new FlowPanel();
	FlowPanel savePanel = new FlowPanel();

    FlowPanel hr = new FlowPanel("hr");

	Button button_save = new Button("Enregistrer");
	Button button_update = new Button("Modifier");
    Button button_retour = new Button("Retour");

    CellTable<VolBean> volsTable;
    ArrayList<VolBean> volsListe;
	VolBean volSelected;

	CellTable<HotelBean> hotelsTable;
	ArrayList<HotelBean> hotelsListe;
	HotelBean hotelSelected;

	CellTable<PanneauBean> panneauTable;
	ArrayList<PanneauBean> panneauListe;
	PanneauBean panneauSelected;
    PanneauBean panneauAfficher;

	Date date;
	DatePicker picker_date = new DatePicker();
	ListBox timePicker = timePicker();

	Label label_titre = new Label();

    Label titre = new Label();
    Label infoVol = new Label();

    Label nomHotel = new Label();
    Label adresseHotel = new Label();
    Label standingHotel = new Label();
    Label tarifHotel = new Label();

    Label date_departVol = new Label();
    Label date_arriveeVol = new Label();
    Label lieu_departVol = new Label();
    Label lieu_arriveeVol = new Label();
    Label tarifVol = new Label();

    FlowPanel titrePanel = new FlowPanel();
    FlowPanel volPanelAffiche = new FlowPanel();
    FlowPanel titrePanelRow = new FlowPanel();
    FlowPanel volPanelRow = new FlowPanel();

    FlowPanel panelImage = new FlowPanel();
    HorizontalPanel panelBas = new HorizontalPanel();
    ListBox listeImage = new ListBox();

    final ListDataProvider<PanneauBean> ldp = new ListDataProvider<>();

    private ListBox timePicker() {
        ListBox lb = new ListBox();

		for (String hour : Constantes.hours) {
			lb.addItem(hour);
		}

		lb.setSelectedIndex(14);

		return lb;
	}

	private void addClickHandler_save(Button button) {
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				save();
			}
		});
	}

    private void addClickHandler_update(Button button) {
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                update();
            }
        });
    }

    private void addClickHandler_retour(Button button) {
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                consultPanel.setVisible(true);
                panneauPanel.add(panelImage);
                afficherPanneau();
                updatePanel.setVisible(false);
                label_titre.setText("Consultation panneau");
            }
        });
    }

	public PanneauWB(int tabPanel) {
		initWidget(mainPanel);
        checkLogin(tabPanel);
    }

    public void checkLogin(final int tabPanel) {
        final AirportServiceAsync connexionIface;
        connexionIface = GWT.create(AirportService.class);

        connexionIface.checkLogin(new AsyncCallback<UserBean>() {

			@Override
			public void onFailure(Throwable caught) {
				infoDanger(caught.toString());
			}

			@Override
			public void onSuccess(UserBean result) {
				if (result != null) {
					labelTitre.getElement().setInnerHTML("Bienvenue " + result.getPrenom() + " " + result.getNom());
					mainPanel.add(label_titre);
					label_titre.setStyleName("nav-header");
					mainPanel.add(hr);
					switch (tabPanel) {
						case 1:
							consultPanel.setVisible(true);
							ajoutPanel.setVisible(false);
							updatePanel.setVisible(false);
							loadConsultPanel();
							break;
						case 2:
							consultPanel.setVisible(false);
							ajoutPanel.setVisible(true);
							updatePanel.setVisible(false);
							loadVols(1);
							break;
						default:
							break;
					}
				} else {
					initbouton(false);
					History.newItem(Constantes.ACCUEILWB);
				}
			}
		});
	}

	private void loadSavePanel() {
		label_titre.setText("Ajout panneau");

		savePanel.add(volPanel);
		savePanel.add(hotelPanel);
        savePanel.add(panelBas);
        mainPanel.add(savePanel);

		button_save = new Button("Enregistrer");

		addClickHandler_save(button_save);
		button_save.setStyleName("btn spaced");
		button_save.setHeight("30");
		button_save.setWidth("140");
		savePanel.add(button_save);

        loadDatePanel();
    }

	private void loadConsultPanel() {
		label_titre.setText("Consultation panneau");

		consultPanel.add(panneauPanel);
		mainPanel.add(consultPanel);

		loadPanneau();
	}

	private void loadUpdatePanel() {
        label_titre.setText("Modification d'un panneau");

		picker_date.setValue(panneauSelected.getDateaff());
		timePicker.setSelectedIndex(panneauSelected.getHeureaff().getHours());

        date = panneauSelected.getDateaff();
        volSelected = panneauSelected.getVol();
        hotelSelected = panneauSelected.getHotel();

		updatePanel.add(volPanel);
		updatePanel.add(hotelPanel);
		mainPanel.add(updatePanel);
        updatePanel.add(panelBas);

		button_update = new Button("Modifier");

		button_update.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				update();
			}
		});

        button_update.setStyleName("btn spaced");
		button_update.setHeight("30");
		button_update.setWidth("140");

        addClickHandler_retour(button_retour);
        button_retour.setStyleName("btn spaced");
        button_retour.setHeight("30");
        button_retour.setWidth("140");

		updatePanel.add(button_update);
        updatePanel.add(button_retour);

		loadDatePanel();

        consultPanel.setVisible(false);
        savePanel.setVisible(false);
        updatePanel.setVisible(true);
    }

	private void loadDatePanel() {
        datePanel.clear();
        dateChanged();
        timePicker.setStyleName("textbox_heure");
		timePicker.setHeight("30");
        //FlowPanel hr = new FlowPanel("hr");


        //panelBas.add(hr);

        //datePanel.add(panelImage);
        datePanel.add(picker_date);
        datePanel.setStyleName("span4");

        listeImage = new ListBox();
        listeImage.addItem("Ville");
        listeImage.addItem("Plage");
        listeImage.addItem("Campagne");
        listeImage.addItem("Montagne");

        // FlowPanel panelImage = new FlowPanel();
        //infoSuccess(panneauSelected.getImage()-1+"");

        if (panneauSelected != null)
            listeImage.setSelectedIndex(panneauSelected.getImage() - 1);

        int image = listeImage.getSelectedIndex() + 1;
        panelImage.setStyleName("span6 panneau" + image + " offset6");

        listeImage.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				afficherPanneauEdit();
			}
		});

        datePanel.add(timePicker);
        datePanel.add(listeImage);

        panelBas.setStyleName("row-fluid");
		panelBas.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		panelBas.add(datePanel);
		panelBas.add(panelImage);

        volPanelAffiche.add(infoVol);

        titrePanel.setStyleName("panneau-titrePre span6 offset3 primary");
        titrePanel.add(titre);

        titrePanelRow.setStyleName(Constantes.ROW_FLUID);
        titrePanelRow.add(titrePanel);

        volPanelRow.setStyleName(Constantes.ROW_FLUID);
        volPanelRow.add(volPanelAffiche);

        panelImage.add(titrePanelRow);
        panelImage.add(volPanelRow);

        //panelImage.setStyleName("span6");

        afficherPanneauEdit();
    }

	private void dateChanged() {
		picker_date.addValueChangeHandler(new ValueChangeHandler<Date>() {
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				date = event.getValue();
			}
		});
	}

	private void loadHotelPanel() {
        hotelPanel.clear();
        hotelsTable = new CellTable<>();

		ListDataProvider<HotelBean> ldp = new ListDataProvider<>();
		ldp.addDataDisplay(hotelsTable);
		ldp.setList(hotelsListe);

		hotelsTable.setStyleName("table table-bordered");

		TextColumn<HotelBean> nom_column = new TextColumn<HotelBean>() {
			@Override
			public String getValue(HotelBean hotel) {
				return hotel.getNom();
			}
		};
		hotelsTable.addColumn(nom_column, "Nom");

		TextColumn<HotelBean> adr_column = new TextColumn<HotelBean>() {
			@Override
			public String getValue(HotelBean hotel) {
				return hotel.getAdresse();
			}
		};
		hotelsTable.addColumn(adr_column, "Adresse");

		Column<HotelBean, Number> std_column = new Column<HotelBean, Number>(new NumberCell()) {
			@Override
			public Integer getValue(HotelBean hotel) {
				return hotel.getStanding();
			}
		};
		hotelsTable.addColumn(std_column, "Standing");

		Column<HotelBean, Number> tarif_column = new Column<HotelBean, Number>(new NumberCell()) {
			@Override
			public Number getValue(HotelBean hotel) {
				return hotel.getTarif();
			}
		};
		hotelsTable.addColumn(tarif_column, "Tarif (€)");

		final SingleSelectionModel<HotelBean> selectionModel = new SingleSelectionModel<>();
		hotelsTable.setSelectionModel(selectionModel);

        boolean hotelExistant = false;

        for (HotelBean hotelbean : hotelsListe) {
            if (panneauSelected != null) {
                if (hotelbean.getIdhotel() == panneauSelected.getHotel().getIdhotel()) {
                    hotelsTable.getSelectionModel().setSelected(hotelbean, true);
                    hotelExistant = true;
                }
            }
        }

        if (!hotelExistant) {
            hotelsTable.getSelectionModel().setSelected(hotelSelected, false);
            hotelSelected = null;
        }

		selectionModel.addSelectionChangeHandler(
				new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						hotelSelected = selectionModel.getSelectedObject();
                        afficherPanneauEdit();
					}
				});

		hotelsTable.setRowCount(hotelsListe.size(), true);
		hotelsTable.setRowData(0, hotelsListe);
		hotelsTable.setKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy.ENABLED);
        FlowPanel hr = new FlowPanel("hr");

        hotelPanel.add(hr);
		hotelPanel.add(hotelsTable);
		SimplePager pager = new SimplePager();
		pager.setDisplay(hotelsTable);
        pager.setPageSize(6);
        hotelPanel.add(pager);


	}

	private void loadVolPanel() {
        volPanel.clear();

		volsTable = new CellTable<>();

		ListDataProvider<VolBean> ldp = new ListDataProvider<>();
		ldp.addDataDisplay(volsTable);
		ldp.setList(volsListe);

		volsTable.setStyleName("table table-bordered");

		TextColumn<VolBean> ld_column = new TextColumn<VolBean>() {
			@Override
			public String getValue(VolBean vol) {
				return vol.getLieu_D();
			}
		};
		volsTable.addColumn(ld_column, "Origine");

		TextColumn<VolBean> la_column = new TextColumn<VolBean>() {
			@Override
			public String getValue(VolBean vol) {
				return vol.getLieu_A();
			}
		};
		volsTable.addColumn(la_column, "Destination");

		TextColumn<VolBean> dd_column = new TextColumn<VolBean>() {
			@Override
			public String getValue(VolBean vol) {
				return dateToString(vol.getDate_D());
			}
		};
		volsTable.addColumn(dd_column, "Départ");

		TextColumn<VolBean> hd_column = new TextColumn<VolBean>() {
			@Override
			public String getValue(VolBean vol) {
				return hourToString(vol.getDate_D());
			}
		};
		volsTable.addColumn(hd_column, "Heure");

		TextColumn<VolBean> da_column = new TextColumn<VolBean>() {
			@Override
			public String getValue(VolBean vol) {
				return dateToString(vol.getDate_A());
			}
		};
		volsTable.addColumn(da_column, "Arrivée");

		TextColumn<VolBean> ha_column = new TextColumn<VolBean>() {
			@Override
			public String getValue(VolBean vol) {
				return hourToString(vol.getDate_A());
			}
		};
		volsTable.addColumn(ha_column, "Heure");

		Column<VolBean, Number> tarif_column = new Column<VolBean, Number>(new NumberCell()) {
			@Override
			public Float getValue(VolBean vol) {
				return vol.getTarif();
			}
		};
		volsTable.addColumn(tarif_column, "Tarif (€)");

		final SingleSelectionModel<VolBean> selectionModel = new SingleSelectionModel<>();
		volsTable.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(
				new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						volSelected = selectionModel.getSelectedObject();
						//
						hotelsListe = new ArrayList<>(volSelected.getHotels());
						//infoSuccess(hotelsListe.toString());
						loadHotelPanel();
						afficherPanneauEdit();
					}
				});

		volsTable.setRowCount(volsListe.size(), true);
		volsTable.setRowData(0, volsListe);
		volsTable.setKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy.ENABLED);

		volPanel.add(volsTable);
		SimplePager pager = new SimplePager();
		pager.setDisplay(volsTable);
		pager.setPageSize(6);
		volPanel.add(pager);
	}

	private String dateToString(Date d) {
		DateTimeFormat dtf = DateTimeFormat.getFormat("dd-MM-yyyy");
		return dtf.format(d);
	}

	private String hourToString(Date d) {
		DateTimeFormat dtf = DateTimeFormat.getFormat("HH:mm");
		return dtf.format(d);
	}

	private void loadPanneauPanel() {
        panneauPanel.clear();
        panneauTable = new CellTable<>();

		ldp.addDataDisplay(panneauTable);
		ldp.setList(panneauListe);

		panneauTable.setStyleName("table table-bordered");

		TextColumn<PanneauBean> origine_column = new TextColumn<PanneauBean>() {
			@Override
			public String getValue(PanneauBean panneau) {
                return panneau.getVol().getLieu_D();
            }
		};
		panneauTable.addColumn(origine_column, "Origine du vol");

		TextColumn<PanneauBean> dest_column = new TextColumn<PanneauBean>() {
			@Override
			public String getValue(PanneauBean panneau) {
                return panneau.getVol().getLieu_A();
            }
		};
		panneauTable.addColumn(dest_column, "Destination du vol");

		TextColumn<PanneauBean> datedepart_column = new TextColumn<PanneauBean>() {
			@Override
			public String getValue(PanneauBean panneau) {
                return dateToString(panneau.getVol().getDate_D());
            }
		};
		panneauTable.addColumn(datedepart_column, "Date départ");

		TextColumn<PanneauBean> hotel_column = new TextColumn<PanneauBean>() {
			@Override
			public String getValue(PanneauBean panneau) {
                return panneau.getHotel().getNom();
            }
		};
		panneauTable.addColumn(hotel_column, "Hotel");

		TextColumn<PanneauBean> dd_column = new TextColumn<PanneauBean>() {
			@Override
			public String getValue(PanneauBean panneau) {
				return dateToString(panneau.getDateaff());
			}
		};
		panneauTable.addColumn(dd_column, "Date d'affichage");

		TextColumn<PanneauBean> hd_column = new TextColumn<PanneauBean>() {
			@Override
			public String getValue(PanneauBean panneau) {
				return hourToString(panneau.getHeureaff());
			}
		};
		panneauTable.addColumn(hd_column, "Heure d'affichage");

        // Bouton editer

        ButtonCell editButton = new ButtonCell() {
            // A native button cell doesn't have the gwt-button class, which makes it
            // look weird next to other gwt buttons. Fix that here.
            @Override
            public void render(
                    final Context context,
                    final SafeHtml data,
                    final SafeHtmlBuilder sb) {
                sb.appendHtmlConstant("<button type=\"button\" class=\"btn btn-default\" tabindex=\"0\">");
                sb.appendHtmlConstant("<span class=\"icon-pencil\"></span>");
                sb.appendHtmlConstant("</button>");
            }
        };

        Column<PanneauBean, String> action_edit_column = new Column<PanneauBean, String>(editButton) {
            @Override
			public String getValue(PanneauBean object) {
				return "~";
			}
		};
		action_edit_column.setFieldUpdater(new FieldUpdater<PanneauBean, String>() {
            @Override
            public void update(int index, PanneauBean panneauBean, String value) {
                panneauSelected = panneauBean;
                loadVols(2);
            }
        });
        panneauTable.addColumn(action_edit_column, "Editer");

        // Bouton supprimer
        ButtonCell supprimeButton = new ButtonCell() {
            // A native button cell doesn't have the gwt-button class, which makes it
            // look weird next to other gwt buttons. Fix that here.
            @Override
            public void render(
                    final Context context,
                    final SafeHtml data,
                    final SafeHtmlBuilder sb) {
                sb.appendHtmlConstant("<center><button type=\"button\" class=\"btn btn-danger\" tabindex=\"0\">");
                sb.appendHtmlConstant("<span class=\"icon-white icon-trash\"></span>");
                sb.appendHtmlConstant("</button></center>");
            }
        };
        Column<PanneauBean, String> action_delete_column = new Column<PanneauBean, String>(supprimeButton) {
            @Override
            public String getValue(PanneauBean object) {
                return "x";
            }
        };
        action_delete_column.setFieldUpdater(new FieldUpdater<PanneauBean, String>() {
			@Override
			public void update(int index, PanneauBean panneauBean, String value) {
				delete(panneauBean, index);
			}
		});
		panneauTable.addColumnStyleName(panneauTable.getColumnCount() - 1, "action-cell");
		panneauTable.addColumnStyleName(panneauTable.getColumnCount(), "action-cell");
        panneauTable.addColumn(action_delete_column, "Supprimer");
        panneauTable.setRowCount(panneauListe.size(), true);
		panneauTable.setRowData(0, panneauListe);
		panneauPanel.add(panneauTable);

		SimplePager pager = new SimplePager();
		pager.setDisplay(panneauTable);
		pager.setPageSize(6);
		panneauPanel.add(pager);

        final SingleSelectionModel<PanneauBean> selectionModelPanneau = new SingleSelectionModel<>();
        panneauTable.setSelectionModel(selectionModelPanneau);
        selectionModelPanneau.addSelectionChangeHandler(
				new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						panneauAfficher = selectionModelPanneau.getSelectedObject();
						afficherPanneau();
					}
				});

        volPanelAffiche.add(infoVol);

        titrePanel.setStyleName("panneau-titrePre span6 offset3 primary");
        titrePanel.add(titre);

        titrePanelRow.setStyleName(Constantes.ROW_FLUID);
        titrePanelRow.add(titrePanel);

        volPanelRow.setStyleName(Constantes.ROW_FLUID);
        volPanelRow.add(volPanelAffiche);

        panelImage.add(titrePanelRow);
        panelImage.add(volPanelRow);

        panneauPanel.add(panelImage);
        if (panneauListe.size() > 0)
            panneauTable.getSelectionModel().setSelected(panneauListe.get(0), true);
        else {
            panelImage.setVisible(false);
        }
    }

    private void afficherPanneau() {
        panelImage.setVisible(true);
		volPanelAffiche.setStyleName("panneau-infoPre" + panneauAfficher.getImage() + " offset1 secondary");
		panelImage.setStyleName("arrondie panneau" + panneauAfficher.getImage() + " span6 offset3");
		lieu_departVol.setText(String.valueOf(panneauAfficher.getVol().getLieu_D()));
        lieu_arriveeVol.setText(String.valueOf(panneauAfficher.getVol().getLieu_A()));
        date_departVol.setText(String.valueOf(panneauAfficher.getVol().getDate_D()));
        date_arriveeVol.setText(String.valueOf(panneauAfficher.getVol().getDate_A()));
        tarifVol.setText(String.valueOf(panneauAfficher.getVol().getTarif()));

        nomHotel.setText(panneauAfficher.getHotel().getNom());
        adresseHotel.setText(panneauAfficher.getHotel().getAdresse());
        standingHotel.setText(String.valueOf(panneauAfficher.getHotel().getStanding()));
        tarifHotel.setText(String.valueOf(panneauAfficher.getHotel().getTarif()));


        // Formatage infos vol
        long duree = (Math.abs(panneauAfficher.getVol().getDate_A().getTime() - panneauAfficher.getVol().getDate_D().getTime()));
        duree /= (24 * 60 * 60 * 1000);

        String date_depart = DateTimeFormat.getFormat("dd/MM/yyyy").format(panneauAfficher.getVol().getDate_D());
        String date_arrivee = DateTimeFormat.getFormat("dd/MM/yyyy").format(panneauAfficher.getVol().getDate_A());
        String tarif = tarifVol.getText().split("\\.")[0];
        String standing = new String();

        for (int i = 0; i < panneauAfficher.getHotel().getStanding(); i++) standing += "★";

        infoVol.setText(
                "Pour un voyage inoubliable" + "\n" +
                        "À l' Hôtel " + standing + " " + nomHotel.getText() + "\n\n" +
                        "Départ de " + lieu_departVol.getText() + " le " + date_depart + "\n" +
                        "Retour de " + lieu_arriveeVol.getText() + " le " + date_arrivee + "\n\n" +
                        "À partir de " + tarif + "€"

        );

        // Titre
        titre.setText("Découvrez\n\t" + lieu_arriveeVol.getText());
    }

    private void afficherPanneauEdit() {

        String standing = new String();
        String date_depart = new String();
        String date_arrivee = new String();
        String tarif = new String();
        int image = listeImage.getSelectedIndex() + 1;
		//label_titre.setText("arrondie panneau" + panneauAfficher.getImage());
		volPanelAffiche.setStyleName("panneau-infoPre" + image + " span6 offset1 secondary");
		panelImage.setStyleName("arrondie panneau" + image + " margin");

        if (volSelected != null) {
            lieu_departVol.setText(String.valueOf(volSelected.getLieu_D()));
            lieu_arriveeVol.setText(String.valueOf(volSelected.getLieu_A()));
            date_departVol.setText(String.valueOf(volSelected.getDate_D()));
            date_arriveeVol.setText(String.valueOf(volSelected.getDate_A()));
            tarifVol.setText(String.valueOf(volSelected.getTarif()));
            date_depart = DateTimeFormat.getFormat("dd/MM/yyyy").format(volSelected.getDate_D());
            date_arrivee = DateTimeFormat.getFormat("dd/MM/yyyy").format(volSelected.getDate_A());
            tarif = tarifVol.getText().split("\\.")[0];
        }

        //panelImage.add(new Label(volSelected.getLieu_D()+""));
        if (hotelSelected != null) {
            nomHotel.setText(hotelSelected.getNom());
            adresseHotel.setText(hotelSelected.getAdresse());
            standingHotel.setText(String.valueOf(hotelSelected.getStanding()));
            tarifHotel.setText(String.valueOf(hotelSelected.getTarif()));


            for (int i = 0; i < hotelSelected.getStanding(); i++) standing += "★";
        }


        // Formatage infos vol
        // long duree = (Math.abs(panneauAfficher.getVol().getDate_A().getTime() - panneauAfficher.getVol().getDate_D().getTime()));
        //duree /= (24 * 60 * 60 * 1000);


        infoVol.setText(
                "Pour un voyage inoubliable" + "\n" +
                        "À l' Hôtel " + standing + " " + nomHotel.getText() + "\n\n" +
                        "Départ de " + lieu_departVol.getText() + " le " + date_depart + "\n" +
                        "Retour de " + lieu_arriveeVol.getText() + " le " + date_arrivee + "\n\n" +
                        "À partir de " + tarif + "€"

        );
        // Titre
        titre.setText("Découvrez\n\t" + lieu_arriveeVol.getText());
    }



	private void loadPanneau() {
		final PanneauServiceAsync panelIface;
		panelIface = GWT.create(PanneauService.class);

		infoDangerHide();
		panelIface.selectAll(new AsyncCallback<ArrayList<PanneauBean>>() {
			@Override
			public void onFailure(Throwable caught) {
				infoDanger(caught.getMessage());
			}

			@Override
			public void onSuccess(ArrayList<PanneauBean> result) {
				panneauListe = result;
				loadPanneauPanel();
			}
		});
	}

    private void loadVols(final int index) {
        final VolServiceAsync volIface;
		volIface = GWT.create(VolService.class);

		infoDangerHide();
		volIface.selectAll(new AsyncCallback<ArrayList<VolBean>>() {
			@Override
			public void onFailure(Throwable caught) {
				infoDanger(caught.getMessage());
			}

			@Override
			public void onSuccess(ArrayList<VolBean> result) {
				volsListe = result;
				loadVolPanel();
                if (panneauSelected != null) {
                    for (VolBean volbean : volsListe) {
                        if (volbean.getId() == panneauSelected.getVol().getId()) {
                            volsTable.getSelectionModel().setSelected(volbean, true);
                            hotelsListe = new ArrayList<>(volbean.getHotels());
                            loadHotelPanel();
                        }
                    }
                }
                if (index == 1)
                    loadSavePanel();
                else
                    loadUpdatePanel();
            }
		});
	}

    private void delete(PanneauBean panneauBean, final int index) {
        final PanneauServiceAsync panneauIface;
		panneauIface = GWT.create(PanneauService.class);

		infoDangerHide();
		panneauIface.delete(panneauBean,
				new AsyncCallback<String>() {
					@Override
					public void onFailure(Throwable caught) {
						infoDanger(caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						infoSuccess(result);
                        ldp.getList().remove(index);
                        ldp.refresh();
                        panneauTable.redraw();
                    }
				});
	}

	private void save() {
        infoDangerHide();
        if (date == null || volSelected == null || hotelSelected == null) {
			infoDanger("Informations incomplètes");
			return;
		}

		PanneauServiceAsync panneauIface;
		panneauIface = GWT.create(PanneauService.class);

        Time heureaff = Time.valueOf(timePicker.getSelectedValue() + ":00");

        PanneauBean panneauBean = new PanneauBean();
        panneauBean.setHotel(hotelSelected);
        panneauBean.setVol(volSelected);
        panneauBean.setDateaff(date);
        panneauBean.setHeureaff(heureaff);
		panneauBean.setImage(listeImage.getSelectedIndex() + 1);


        panneauIface.save(panneauBean,
                new AsyncCallback<String>() {
					@Override
					public void onFailure(Throwable caught) {
                        infoDanger(volSelected.getId() + " " + hotelSelected.getIdhotel());
                        //infoDanger(caught.getMessage());
                    }

					@Override
					public void onSuccess(String result) {
						infoSuccess(result);
					}
				});
	}

    private void update() {
        infoDangerHide();

        if (date == null || volSelected == null || hotelSelected == null) {
            infoDanger("Informations incomplètes");
            return;
        }

		final PanneauServiceAsync panneauIface;
		panneauIface = GWT.create(PanneauService.class);

        Time heureaff = Time.valueOf(timePicker.getSelectedValue() + ":00");

        panneauSelected.setHotel(hotelSelected);
        panneauSelected.setVol(volSelected);
        panneauSelected.setHeureaff(heureaff);
        panneauSelected.setDateaff(date);
		panneauSelected.setImage(listeImage.getSelectedIndex() + 1);
        panneauIface.update(panneauSelected,
                new AsyncCallback<String>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        infoDanger(caught.getMessage());
                    }

                    @Override
                    public void onSuccess(String result) {
                        infoSuccess(result);
						consultPanel.setVisible(true);
						updatePanel.setVisible(false);
						panneauTable.redraw();
						panneauPanel.add(panelImage);
						afficherPanneau();
                        label_titre.setText("Consultation panneau");
                    }
                });
    }
}
